package saolei;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Scanner;

public class Records extends ArrayList<Record> {
    public void readRecords(String infile) throws FileNotFoundException, IOException {
        // 从文件中读取records
        MyLogger.logger.info("读取记录");
        try {
            Scanner cin=new Scanner(infile);
            while (true) {
                ArrayList<String> info=readln(cin);
                String id=info.get(0);
                String name=info.get(1);
                int time=Integer.parseInt(info.get(2));
                Record r=new Record(id, name, time);
                this.add(r);
            }
        } catch (Exception e) {
            // do nothing
        }
        return;
    }

    public void insertRecord(Record r) {
        // 根据r的compareTo()方法插入r
        MyLogger.logger.info("插入记录");
        int i = 0;
        while (i < this.size() && r.compareTo(this.get(i)) > 0) {
            i++;
        }
        this.add(i, r);
    }

    public void writeRecords(String outfile) throws FileNotFoundException, IOException {
        // 将records写入文件(一份保存对象，一份可读字符串)
        OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(outfile), "UTF-8");
        for (Record r : this) {
            out.write(r.toString());
            out.write("\n");
        }
        out.close();
        return;
    }

    public Records getRecords(String id) {
        // 返回所有该id的记录
        MyLogger.logger.info("查找 " + id + " 的通关记录");
        Records result = new Records();
        for (Record r : this) {
            if (r.getId().equals(id)) {
                result.add(r);
            }
        }
        return result;
    }

    private ArrayList<String> readln(Scanner cin) {
        String str = cin.nextLine();
        ArrayList<String> info = new ArrayList<String>();
        while (!str.equals("")) {
            int index = str.indexOf(" ");
            if (index >= 0) {
                String current = str.substring(0, index);
                if (!current.equals(""))
                    info.add(current);
                str = str.substring(index + 1);
            } else {
                info.add(str);
                break;
            }
        }
        return info;
    }
}

class Record implements Serializable, Comparable<Record> {
    // 通关记录，每一次的通关情况
    private String id;// 游戏者id
    private String name;
    private Integer time;// 这里指完成游戏的时间
    private static final long serialVersionUID = 1L;

    public Record(String id, String name, int time) {
        this.id = id;
        this.name = name;
        this.time = time;
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public int getTime() {
        return time;
    }

    @Override
    public String toString() {
        return "账号：" + id + ", 姓名：" + name + ", 通关时间：" + time;
    }

    @Override
    public int compareTo(Record o) {
        // 根据socre比较o与this
        return o.time.compareTo(this.time);
    }
}